package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoopingCatMapper;
import com.sky.service.ShoppingCatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCatServiceImpl implements ShoppingCatService {
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private DishMapper dishMapper;


    @Autowired
    private ShoopingCatMapper shoopingCatMapper;

    /*添加购物车*/
    @Override
    public void addShoppingCat(ShoppingCartDTO shoppingCatDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCatDTO, shoppingCart);

        //从拦截器中获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //判断当前加入到购物车中的商品是否已经存在
        List<ShoppingCart> shoppingCartList = shoopingCatMapper.getList(shoppingCart);
        if (shoppingCartList.size() > 0 && shoppingCartList != null) {//如果商品已经存在，则更新购物车中该商品的数量
            //如果商品已经存在，则获取当前集合的第一个商品数据(一般查询出来也就只会有一个)
            ShoppingCart cart = shoppingCartList.get(0);
            //将商品数量加一
            cart.setNumber(cart.getNumber() + 1);
            shoopingCatMapper.update(cart);
        } else {//如果商品不存在，则将商品添加到购物车中
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            //判断当前商品是否为套餐还是菜品
            if (shoppingCatDTO.getDishId() != null) {
                //当前商品为菜品
                Dish disById = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setAmount(disById.getPrice());
                shoppingCart.setImage(disById.getImage());
                shoppingCart.setName(disById.getName());

            } else {
                //当前商品为套餐
                Setmeal setmealById = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmealById.getPrice());
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setName(setmealById.getName());

            }

            shoopingCatMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCat() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoopingCatMapper.getList(shoppingCart);
        return list;
    }

    @Override
    public void deleteShoppingCat() {
        shoopingCatMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCatDTO) {
        //先查询购物车中该商品信息
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCatDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoopingCatMapper.getList(shoppingCart);

        if (list.size() > 0 && list != null) {
            //如果商品存在，则获取购物车中的商品信息
            ShoppingCart cart = list.get(0);

            if (cart.getNumber() == 1) {//如果是一条数据，则直接删除
                shoopingCatMapper.delete(cart.getId());
            } else {//如果商品数量大于1，则数量减一
                cart.setNumber(cart.getNumber() - 1);
                shoopingCatMapper.update(cart);
            }
        }
    }
}
